home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / db / esm-3.1 / esm-3 / usr / local / sm / src / serverlib / msg / clientReceive.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-05-05  |  5.7 KB  |  205 lines

  1. /*
  2.  *   $RCSfile: clientReceive.c,v $  
  3.  *   $Revision: 1.1.1.1 $  
  4.  *   $Date: 1996/05/04 21:55:54 $      
  5.  */ 
  6. /**********************************************************************
  7. * EXODUS Database Toolkit Software
  8. * Copyright (c) 1991 Computer Sciences Department, University of
  9. *                    Wisconsin -- Madison
  10. * All Rights Reserved.
  11. *
  12. * Permission to use, copy, modify and distribute this software and its
  13. * documentation is hereby granted, provided that both the copyright
  14. * notice and this permission notice appear in all copies of the
  15. * software, derivative works or modified versions, and any portions
  16. * thereof, and that both notices appear in supporting documentation.
  17. *
  18. * THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY OF WISCONSIN --
  19. * MADISON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" CONDITION.  
  20. * THE DEPARTMENT DISCLAIMS ANY LIABILITY OF ANY KIND FOR ANY DAMAGES
  21. * WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
  22. *
  23. * The EXODUS Project Group requests users of this software to return 
  24. * any improvements or extensions that they make to:
  25. *
  26. *   EXODUS Project Group 
  27. *     c/o David J. DeWitt and Michael J. Carey
  28. *   Computer Sciences Department
  29. *   University of Wisconsin -- Madison
  30. *   Madison, WI 53706
  31. *
  32. *     or exodus@cs.wisc.edu
  33. *
  34. * In addition, the EXODUS Project Group requests that users grant the 
  35. * Computer Sciences Department rights to redistribute these changes.
  36. **********************************************************************/
  37.  
  38. #include "sysdefs.h"
  39. #include "ess.h"
  40. #include "checking.h"
  41. #include "trace.h"
  42. #include "error.h"
  43. #include "list.h"
  44. #include "tid.h"
  45. #include "io.h"
  46. #include "lock.h"
  47. #include "object.h"
  48. #include "msgdefs.h"
  49. #include "thread.h"
  50. #include "semaphore.h"
  51. #include "link.h"
  52. #include "host.h"
  53. #include "bitvec.h"
  54. #include "msgvector.h"
  55. #include "msg_funcs.h"
  56. #include "util_funcs.h"
  57. #include "thread_funcs.h"
  58. #include "thread_globals.h"
  59. #include "msg_globals.h"
  60.  
  61.  
  62. extern int            errno;
  63.  
  64.  void
  65. clientReceive (
  66.  
  67.     register LINK        *link 
  68. )
  69. {
  70.  
  71.     register MESSAGE    *message;
  72.     register MSGVECTOR    *msgVector;
  73.     register TCB        *tcb;
  74.  
  75.  
  76.     TRPRINT(TR_MSG, TR_LEVEL_1, ("got message on link:%d", link->id));
  77.  
  78.     /*
  79.      *    check to see if there is a waiter on that link
  80.      */
  81.     if ((tcb = (TCB *) listDeq( &(link->tcbList) )) != NULL)    {
  82.  
  83.         TRPRINT(TR_MSG, TR_LEVEL_2, ("have waiting task:%d", tcb->id));
  84.         
  85.         /*
  86.          *    switch to that thread and restart the Active thread
  87.          */
  88.         freeThread(Active);
  89.         threadSwitch(tcb);
  90.         threadRestart();
  91.     }
  92.  
  93.     /*
  94.      *    get a register pointer to the message buffer
  95.      */
  96.     message = &(Active->message);
  97.  
  98.     /*
  99.      *    Read the message.
  100.      *  Since the client could be sending us a MESSAGE that is 
  101.      *  smaller than our idea of a MESSAGE (e.g. a message of a
  102.      *  different protocol version), we must NOT block trying
  103.      *  to read a full MESSAGE.
  104.      *
  105.      *  Thus, if we get a short read, we must check the PROTOCOL_VERSION 
  106.      *  before we go on to read the rest of the MESSAGE.
  107.      *  It is sufficient to check only the very first PROTOCOL_VERSION
  108.      *  separately, because TCP guarantees that we will not get 
  109.      *  messages from sources other than the client peer.
  110.      */
  111.     if( link->flags == LINK_ACCEPT ) {
  112.         if (receive(link, (char *) message, 
  113.             sizeof(message->header.version)) < 0){
  114.             TRPRINT(TR_MSG, TR_LEVEL_2, ("receive failed:%d", errno));
  115.             threadRestart();
  116.         }
  117.         if (message->header.version != MESSAGE_VERSION)    {
  118.             SM_ERROR(TYPE_USER, esmPROTOCOLVERSION);
  119.             reply(esmFAILURE, Active->errno, 1, TRUE);
  120.             /* hope that the client shuts the cx */
  121.         }
  122.  
  123.         if (receive(link, ((char *) message)+ sizeof(message->header.version), 
  124.             (sizeof(MESSAGE)-sizeof(message->header.version))) < 0)    {
  125.             TRPRINT(TR_MSG, TR_LEVEL_2, ("receive failed:%d", errno));
  126.             threadRestart();
  127.         }
  128.  
  129.         link->flags = LINK_RECV;
  130.     } else {
  131.         /* 
  132.          * we already know that are speaking the same protocol 
  133.          * so do not bother to check the version in a separate
  134.          * read request.
  135.          */
  136.         if (receive(link, (char *) message, sizeof(MESSAGE)) < 0)    {
  137.             TRPRINT(TR_MSG, TR_LEVEL_2, ("receive failed:%d", errno));
  138.             threadRestart();
  139.         }
  140.         /*
  141.          *    Check to see if the message magic is valid
  142.          *  At this point, the MESSAGE_VERSION is acting
  143.          *  as a magic number.  This will catch such surprises
  144.          *  as a client having a longer appended part (from the previous
  145.          *  message) than we expected, resulting in our reading
  146.          *  what we think should be a MESSAGE, but is in fact the
  147.          *  rest of the previous (appended part of the) message.
  148.          */
  149.         if (message->header.version != MESSAGE_VERSION)    {
  150.  
  151.             SM_ERROR(TYPE_USER, esmBADMESSAGEMAGIC);
  152.             reply(esmFAILURE, Active->errno, 1, TRUE);
  153.             /* hope that the client shuts the cx */
  154.         }
  155.     }
  156.  
  157.     TRPRINT(TR_MSG, TR_LEVEL_2, ("got message of type:%d", 
  158.         message->header.type));
  159.  
  160.     /*
  161.      *    save the link that the request came in on
  162.      */
  163.     Active->linkId = link->id;
  164.  
  165.     if( !(message->header.replyRequested) )
  166.         MsgStats.no_reply_requested[message->header.type]++;
  167.     MsgStats.received [message->header.type]++;
  168.  
  169.  
  170.     /*
  171.      *    check to see if the message header type is correct
  172.      */
  173.     if (message->header.type > MAX_MSG_NUMBER)    {
  174.  
  175.         /*
  176.          *    send error back to user
  177.          */
  178.         SM_ERROR(TYPE_USER, esmBADMESSAGENUMBER);
  179.         reply(esmFAILURE, Active->errno, 1, TRUE);
  180.     }
  181.  
  182.     /*
  183.      *    get a pointer to the message function table
  184.      */
  185.     msgVector = &(MsgVectorTable[message->header.type]);
  186.     SM_ASSERT(LEVEL_1, (msgVector->msgtype == message->header.type));
  187.  
  188.     /*
  189.      *    check to see that the class is appropriate
  190.      */
  191.     if (msgVector->linkClass != link->linkClass)    {
  192.  
  193.         TRPRINT(TR_MSG, TR_LEVEL_2, ("bad link class: message type %d", 
  194.             message->header.type));
  195.         SM_ERROR(TYPE_FATAL, esmINTERNAL);
  196.     }
  197.  
  198.     /*
  199.      *    call the function
  200.      */
  201.     (*(msgVector->function))(message, link);
  202.  
  203.     /* ok to get here now.  Return to main loop */
  204. }
  205.